<?php
namespace Model;

use Core\Config;
use Core\Exception\SystemException;
use Model\Factory\TableGatewayFactory;

class Page extends AbstractModel
{
    public $id;
    public $name;
    public $summary;
    public $template_id;
    public $privilege_id;
    public $is_publish_for_all;
    public $is_log;
    public $title;
    public $module;
    public $controller;
    public $action;
    public $params;
    public $created_date;
    public $created_by;
    public $active;


    public function getAllBlocks()
    {
        $blockInstanceTable = TableGatewayFactory::getBlockInstanceTable();
        $select = $blockInstanceTable->getDbSqlSelect();
        $select->join(array("tp"=>"page_block"), 'tp.block_id = block_instance.id',
            array("div_id"=>"div_id"))
            ->where(array('page_id'=>$this->id));
        $allBlock = $blockInstanceTable->getTableGateway()->selectWith($select);
        $resultArr = array();
        foreach ($allBlock as $aBlock) {
            array_push($resultArr, $aBlock);
        }
        return $resultArr;
    }

    /**
     * @return Route
     */
    public function getRoute()
    {
        $routeTable = TableGatewayFactory::getRouteTable(Config::getDefaultLanguage());
        return $routeTable->getRouteByPageId($this->id);
    }



    /**
     * @return Template
     */
    public function getTemplate()
    {

        $templateTable = TableGatewayFactory::getTemplateTable(Config::getDefaultLanguage());
        return $templateTable->getTemplate($this->template_id);

    }

    public function addBlock($divId, $blockInstanceId){

        $pageBlockTable = TableGatewayFactory::getPageBlockTable();
        if($this->getBlockById($blockInstanceId, $divId))
            return;
        $pageBlockTable->insert(array("page_id"=>$this->id,"block_id"=>$blockInstanceId,"div_id"=>$divId));

    }

    /**
     * @param $blockInstanceId
     * @param $divId
     * @return null|BlockInstance
     */
    public function getBlockById($blockInstanceId,$divId){

        $blockInstanceTable = TableGatewayFactory::getBlockInstanceTable();
        $select = $blockInstanceTable->getDbSqlSelect();
        $select->join(array('pb'=>'page_block'), 'pb.block_id = block_instance.id', array())
            ->where(array('page_id'=>$this->id))
            ->where(array('div_id'=>$divId))
            ->where(array('block_id'=>$blockInstanceId));
        $allBlock = $blockInstanceTable->getTableGateway()->selectWith($select);
        if($allBlock->count() == 0)
            return null;
        else
            return $allBlock[0];

    }

    public function updateRoute($route, $name){
        $myRoute = $this->getRoute();
        $routeTable = TableGatewayFactory::getRouteTable(Config::getDefaultLanguage());
        if(!$myRoute) throw new SystemException('Page không có route kèm theo.');
        $routeTable->update(array('route'=>$route, 'name'=>$name),$myRoute->id);
    }


    public function updatePrivilege($privilegeCode){

        $privilegeTable = TableGatewayFactory::getPrivilegeTable();
        $privilegeTable->update(array('code'=>$privilegeCode), $this->privilege_id);
    }

    public function detachAllBlock(){
        $pageBlockTable = TableGatewayFactory::getPageBlockTable();
        $pageBlockTable->getTableGateway()->delete('page_id = '.$this->id);
    }

    public function getAllGrantedRole(){

        $roleTable = TableGatewayFactory::getRoleTable();
        $select = $roleTable->getDbSqlSelect();
        $select->join(array('rp'=>'role_privilege'), 'rp.role_id = role.id', array())
            ->where(array('rp.privilege_id'=>$this->privilege_id))
        ->where(array('rp.active'=>1));
        $result = array();
        $roleResultSet = $roleTable->getTableGateway()->selectWith($select);
        foreach ($roleResultSet as $aRole) {
            array_push($result, $aRole);
        }

        return $result;

    }

    /**
     * @return Privilege
     */
    public function getPrivilege(){
        $privilegeTable = TableGatewayFactory::getPrivilegeTable();
        return $privilegeTable->getPrivilege($this->privilege_id);
    }






}
